package com.taotao.rop.service;

import com.rop.RopRequest;
import com.rop.annotation.HttpAction;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.rop.session.SimpleSession;
import com.taotao.rop.database.domain.Menu;
import com.taotao.rop.database.domain.RoleMenu;
import com.taotao.rop.database.mapper.MenuMapper;
import com.taotao.rop.database.mapper.RoleMenuMapper;
import com.taotao.rop.manager.RopSessionManager;
import com.taotao.rop.response.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@ServiceMethodBean(httpAction = HttpAction.POST, version = "1.0")
public class MenuService {
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private MenuMapper menuMapper;

    @ServiceMethod(method = "menu.show")
    public Object showMenu(RopRequest request){
        SimpleSession session = (SimpleSession) request.getRopRequestContext().getSession();
        Long roleId = (Long) session.getAttribute(RopSessionManager.KEY_ROLE_ID);
        RoleMenu condition = new RoleMenu();
        condition.setRoleId(roleId);
        List<RoleMenu> roleMenuList = roleMenuMapper.getList(condition);

        Menu menuCondition = new Menu();
        menuCondition.setParentId(0L);
        List<Menu> parentMenuList = menuMapper.getList(menuCondition);
        List<Menu> theParentMenuList = new ArrayList<Menu>();
        for (Menu parentMenu : parentMenuList) {
            menuCondition.setParentId(parentMenu.getId());
            List<Menu> subMenuList = menuMapper.getList(menuCondition);
            List<Menu> theSubMenuList = new ArrayList<Menu>();
            for (Menu subMenu : subMenuList) {
                if (subMenu.haveRole(roleMenuList)){
                    theSubMenuList.add(subMenu);
                }
            }
            if (theSubMenuList.size() > 0) {
                parentMenu.setSubMenus(theSubMenuList);
                theParentMenuList.add(parentMenu);
            }
        }

        BaseResponse<List<Menu>> response = new BaseResponse<List<Menu>>();
        response.setSuccess(true);
        response.setMsg("" + theParentMenuList.size());
        response.setData(theParentMenuList);
        return response;
    }

    @ServiceMethod(method = "menu.list")
    public Object listMenu(Menu request) {
        Menu menuCondition = new Menu();
        menuCondition.setParentId(0L);
        List<Menu> parentMenuList = menuMapper.getList(menuCondition);
        List<Menu> theParentMenuList = new ArrayList<Menu>();
        for (Menu parentMenu : parentMenuList) {
            menuCondition.setParentId(parentMenu.getId());
            List<Menu> subMenuList = menuMapper.getList(menuCondition);
            List<Menu> theSubMenuList = new ArrayList<Menu>();
            for (Menu subMenu : subMenuList) {
                theSubMenuList.add(subMenu);
            }
            parentMenu.setSubMenus(theSubMenuList);
            theParentMenuList.add(parentMenu);
        }

        BaseResponse<List<Menu>> response = new BaseResponse<List<Menu>>();
        response.setSuccess(true);
        response.setMsg("" + theParentMenuList.size());
        response.setData(theParentMenuList);
        return response;
    }

    @ServiceMethod(method = "menu.insert")
    public Object insertMenu(Menu request) {
        Integer count = menuMapper.insertOne(request);

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    @ServiceMethod(method = "menu.update")
    public Object updateMenu(Menu request) {
        Integer count = menuMapper.updateOne(request);

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }
}
